<?php
/**
 * MatchDart! is a darts event ranking table component for joomla
 * 
 * @version		2.0.1 | $Id: teams.php 116 2009-10-12 14:08:54Z mwi.nor $
 * @package		MatchDart!
 * @subpackage	Admin
 * @copyright	Copyright (C) 2005 - 2009 Marco Wilde. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * @author		Marco Wilde, <mailto://info@matchdart.de>
 * @link		http://www.matchdart.de
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

class MatchdartModelTeams extends JModel
{
    protected $_data;
    protected $_total;
    protected $_pagination;

    public function __construct()
    {
        parent::__construct();

        $app = &JFactory::getApplication();

        $config = JFactory::getConfig();

        // Get the pagination request variables
        $limit		= $app->getUserStateFromRequest( 'matchdart.teams.limit', 'limit', $app->getCfg('list_limit'), 'int' );
        $limitstart	= $app->getUserStateFromRequest( 'matchdart.teams.limitstart', 'limitstart', 0, 'int' );

        // In case limit has been changed, adjust limitstart accordingly
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

        $this->setState('matchdart.teams.limit', $limit);
        $this->setState('matchdart.teams.limitstart', $limitstart);	}

    protected function _buildQuery()
    {

        $query = 'SELECT'. 
        			' T.*,'.
        			' V.VenueName as TeamVenueName,'.
        			' S.TeamName as TeamSubstituteName'. 
        		  ' FROM'. 
        		  	' #__md_teams T'. 
        		  	' LEFT JOIN #__md_venues V on (V.VenueID = T.TeamVenueID)'.
        		  	' LEFT JOIN #__md_teams S on (S.TeamID = T.TeamSubstitute)'. 
        		  ' ORDER BY TeamName';
        
        return $query;
    }

    public function getData()
    {
        // Lets load the data if it doesn't already exist
        if (empty( $this->_data ))
        {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList( $query, $this->getState('matchdart.teams.limitstart'), $this->getState('matchdart.teams.limit') );
        }
        return $this->_data;
    }

    public function getPagination()
    {
        if (empty($this->_pagination))
        {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination( $this->getTotal(), $this->getState('matchdart.teams.limitstart'), $this->getState('matchdart.teams.limit') );
        }

        return $this->_pagination;
    }

    public function getTotal()
    {
        if (empty($this->_total))
        {
            $query = $this->_buildQuery();
            $this->_total = $this->_getListCount($query);
        }

        return $this->_total;
    }
 
}
?>